﻿using PaymentServiceFactory.Services.Interfaces;

namespace PaymentServiceFactory.Services
{
    /// <summary>
    /// 业务逻辑层
    /// </summary>
    //public class PaymentProcessor
    //{
    //    private readonly PaymentServiceFactorySimple _factory;
    //    private readonly ILogger<PaymentProcessor> _logger;

    //    public PaymentProcessor(PaymentServiceFactorySimple factory, ILogger<PaymentProcessor> logger)
    //    {
    //        _factory = factory;
    //        _logger = logger;
    //    }

    //    public string ProcessPayment(string paymentType, decimal amount)
    //    {
    //        try
    //        {
    //            var paymentService = _factory.CreatePaymentService(paymentType);
    //            return paymentService.ProcessPayment(amount);
    //        }
    //        catch (Exception ex)
    //        {
    //            _logger.LogError(ex, "支付处理失败: {PaymentType}, 金额: {Amount}", paymentType, amount);
    //            return $"支付处理失败: {ex.Message}";
    //        }
    //    }
    //}
    //public class PaymentProcessor
    //{
    //    private readonly PaymentServiceFactorySimple _factory;
    //    private readonly ILogger<PaymentProcessor> _logger;

    //    public PaymentProcessor(PaymentServiceFactorySimple factory, ILogger<PaymentProcessor> logger)
    //    {
    //        _factory = factory;
    //        _logger = logger;
    //    }

    //    public async Task<string> ProcessPaymentAsync(string paymentType, string orderId, decimal amount)
    //    {
    //        try
    //        {
    //            var paymentService = _factory.Create(paymentType, orderId);

    //            // 正确使用 await 调用异步方法
    //            return await paymentService.ProcessPayment(amount);
    //        }
    //        catch (Exception ex)
    //        {
    //            _logger.LogError(ex, "支付处理失败: {PaymentType}, 订单: {OrderId}", paymentType, orderId);
    //            return $"支付处理失败: {ex.Message}";
    //        }
    //    }
    //}
    //public class PaymentProcessor
    //{
    //    private readonly PaymentServiceFactorySimple _factory;
    //    private readonly ILogger<PaymentProcessor> _logger;

    //    public PaymentProcessor(PaymentServiceFactorySimple factory, ILogger<PaymentProcessor> logger)
    //    {
    //        _factory = factory;
    //        _logger = logger;
    //    }

    //    public async Task<string> ProcessPaymentAsync(string paymentType, string orderId, decimal amount)
    //    {
    //        try
    //        {
    //            var paymentService = _factory.Create(paymentType);

    //            // 将 orderId 作为方法参数传递
    //            return await paymentService.ProcessPayment(orderId, amount);
    //        }
    //        catch (Exception ex)
    //        {
    //            _logger.LogError(ex, "支付处理失败: {PaymentType}, 订单: {OrderId}", paymentType, orderId);
    //            return $"支付处理失败: {ex.Message}";
    //        }
    //    }
    //    //public async Task<string> ProcessPaymentAsync(string paymentType, string orderId, decimal amount)
    //    //{
    //    //    try
    //    //    {
    //    //        // 将 orderId 作为方法参数传递
    //    //        return await _paymentService.ProcessPayment(orderId, amount);
    //    //    }
    //    //    catch (Exception ex)
    //    //    {
    //    //        _logger.LogError(ex, "支付处理失败: {PaymentType}, 订单: {OrderId}", paymentType, orderId);
    //    //        return $"支付处理失败: {ex.Message}";
    //    //    }
    //    //}
    //}

    public class PaymentProcessor
    {
        private readonly PaymentServiceFactorySimple _factory;
        private readonly ILogger<PaymentProcessor> _logger;

        public PaymentProcessor(PaymentServiceFactorySimple factory, ILogger<PaymentProcessor> logger)
        {
            _factory = factory;
            _logger = logger;
        }

        public async Task<string> ProcessPaymentAsync(string paymentType, string orderId, decimal amount)
        {
            try
            {
                var paymentService = _factory.Create(paymentType);
                return await paymentService.ProcessPayment(orderId, amount);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "支付处理失败: {PaymentType}, 订单: {OrderId}", paymentType, orderId);
                return $"支付处理失败: {ex.Message}";
            }
        }
    }
}
